package com.bishe.supermarket.service;

import com.bishe.supermarket.mapper.SaveMapper;
import com.bishe.supermarket.pojo.Save;
import com.bishe.supermarket.pojo.vo.Status;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ShopService {

    @Autowired
    SaveMapper saveMapper;

    //插入到数据库，进货

    public Status jinHuo(int storeId,String comName,String comCategory,int comNum,int comPrimeCost,int comPrice){

        Status status = new Status();

        if (storeId<=0){
            status.setMsg("请输入合法仓库编号！");
            status.setCode(false);
            return status;
        }
        if(comNum<=0){
            status.setCode(false);
            status.setMsg("请输入合法商品数量！");
            return status;
        }
        if(comPrimeCost<=0){
            status.setCode(false);
            status.setMsg("请输入合法商品进价！");
            return status;
        }
        if(comPrice<=0){
            status.setCode(false);
            status.setMsg("请输入合法商品售价！");
            return status;
        }
        //查看进货的商品，数据库中有没有，有的话，直接修改数量就行了
        //如果没有，则直接插入该货物

        Save save = saveMapper.select(comName);

        if (save!=null){
            save.setCommodityNum(save.getCommodityNum()+comNum);

            saveMapper.update(save);

        }
        if (save==null){
            Save save1 = new Save();
            save1.setCommodityName(comName);
            save1.setCommodityNum(comNum);
            save1.setCommodityCategory(comCategory);
            save1.setCommodityPrice(comPrice);
            save1.setCommodityPrimeCost(comPrimeCost);
            save1.setStoreId(storeId);
            saveMapper.insert(save1);

        }
        status.setMsg("进货成功！");
        status.setCode(true);

        return status;
    }

    //把仓库有的商品返回
    public List<Save> backCom(){

        List<Save> list = saveMapper.selectAll();

        return list;
    }

    //退货处理

    public Status backHuo(String comName,int comNum){

        Status status = new Status();
        if (comNum<=0){
            status.setMsg("请输入合法商品数量！");
            status.setCode(false);
            return status;
        }

        List<Save> list = saveMapper.seleceByName(comName);

        System.out.println(list.get(0).getCommodityNum());
        System.out.println("我是退货数量"+comNum);
        if (comNum>list.get(0).getCommodityNum()){
            status.setMsg("退货数量大于库存数量！");
            status.setCode(false);
            return status;
        }

        //对仓库退货之后的商品进行处理
        if(list.get(0).getCommodityNum()-comNum==0){
            //删除该商品
            saveMapper.delete(comName);
        }
        if (list.get(0).getCommodityNum()-comNum>0){
            Save save = new Save();
            save.setCommodityName(comName);
            save.setCommodityNum(list.get(0).getCommodityNum()-comNum);

            saveMapper.update(save);

        }
        status.setCode(true);
        status.setMsg("回退成功！");

        return status;

    }
}
